/**
 * 
 */
package it.kion.util.ui.ulog2.views;

import it.kion.util.ui.ulog2.Log4jPlugin;
import it.kion.util.ui.ulog2.Log4jUtil;

import org.apache.log4j.Level;
import org.apache.log4j.spi.LoggingEvent;
import org.eclipse.jface.viewers.TableViewer;
import org.eclipse.swt.widgets.Table;
import org.eclipse.swt.widgets.TableItem;

/**
 * @author Andrea Salicetti &lt;<a
 *         href="mailto:asalicetti@kion.it">asalicetti@kion.it</a>&gt;
 * 
 */
public class Log4jTableAdderThread implements Runnable {

	private LoggingEvent event;

	public Log4jTableAdderThread(LoggingEvent event) {
		this.event = event;
	}

	public void run() {
		Log4jView view = Log4jPlugin.getMainView();
		Table table = null;
		TableViewer tableViewer = null; 
		if(view!=null) {
			table = view.getTable();
			tableViewer = view.getViewer();
		}
		if(table!=null && tableViewer!=null) {
			tableViewer.add(event);
			TableItem tableItem = new TableItem(table, 0, 0);

			int priority = event.getLevel().toInt();
			switch (priority) {
			case Level.DEBUG_INT:
				tableItem.setImage(0, Log4jView.DEBUG_IMG);
				break;
			case Level.INFO_INT:
				tableItem.setImage(0, Log4jView.INFO_IMG);
				break;
			case Level.WARN_INT:
				tableItem.setImage(0, Log4jView.WARN_IMG);
				break;
			case Level.ERROR_INT:
				tableItem.setImage(0, Log4jView.ERROR_IMG);
				break;
			case Level.FATAL_INT:
				tableItem.setImage(0, Log4jView.FATAL_IMG);
				break;
			default:
				;
			}
			for (int i = 1; i < table.getColumnCount(); i++) {
				tableItem.setText(i, (((ViewLabelProvider) tableViewer.getLabelProvider()).getColumnText(event, i)));
			}
			tableItem.setForeground(Log4jUtil.getColorFG(event.getLevel()));
			tableItem.setBackground(Log4jUtil.getColorBG(event.getLevel()));
		}
	}

}
